package cn.coldleaf.wiki.utils;

import java.util.Random;

/**
 * 字符串工具类
 * @datetime 2023/10/15 上午11:56
 * @user cold
 **/
public class StringUtil {

    /**
     * byte数组转16进制字符串
     * @datetime 2023-10-15 12:22:33
     * @user cold
     **/
    public static String bytes2HexString(byte[] bytes){
        StringBuilder hexStr = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String tmp = Integer.toHexString(bytes[i] & 0xFF);
            if (tmp.length() == 1){
                hexStr.append(0);
            }
            hexStr.append(tmp);
        }
        return hexStr.toString();
    }

    /**
     * 16进制转byte数组
     * @datetime 2023-10-15 12:30:56
     * @user cold
     **/
    public static byte[] hexString2bytes(String hexStr){
        byte[] bytes = new byte[hexStr.length()/2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(hexStr.substring(i*2,i*2+2),16);
        }
        return bytes;
    }

    /**
     * 判断字符串是否为空(null || "")
     * @param str 待判断字符串
     * @datetime 2023-10-15 12:34:47
     * @user cold
     **/
    public static boolean isBlank(String str){
        return str == null || str.trim().length() == 0;
    }

    /**
     * 判断字符串是否不为空(null || "")
     * @param str 待判断字符串
     * @datetime 2023-10-15 12:36:15
     * @user cold
     **/
    public static boolean isNotBlank(String str){
        return !isBlank(str);
    }

    /**
     * 判断字符串是否为空(null)
     * @param str 待判断字符串
     * @datetime 2023-10-15 12:40:03
     * @user cold
     **/
    public static boolean isNull(String str){
        return str ==null;
    }

    /**
     * 判断字符串是否不为空(null)
     * @param str 待判断字符串
     * @datetime 2023-10-15 12:40:34
     * @user cold
     **/
    public static boolean isNotNull(String str){
        return str !=null;
    }

    /**
     * 判断字符串是否为空("")
     * @param str 待判断字符串
     * @datetime 2023-10-15 12:41:08
     * @user cold
     **/
    public static boolean isEmpty(String str){
        return str != null && str.trim().length() == 0;
    }

    /**
     * 判断字符串是否不为空("")
     * @param str 待判断字符串
     * @datetime 2023-10-15 12:41:38
     * @user cold
     **/
    public static boolean isNotEmpty(String str){
        return str != null && str.trim().length() > 0;
    }

    public static boolean equals(String str1,String str2){
        if (str1 == null && str2 ==null){
            return true;
        }
        if (str1 == null || str2 == null){
            return false;
        }
        return str1.equals(str2);

    }

    private static String randomStr(String baseChar,int length){
        Random random = new Random(System.currentTimeMillis());
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < length; i++) {
            result.append(baseChar.charAt(random.nextInt(baseChar.length())));
        }
        return result.toString();
    }

    public static String randomStr(int length){
        String baseChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return randomStr(baseChar,length);
    }

    public static String randomIgnoreSimilarStr(int length){
        String baseChar = "abdefghijmnqrtuyABDEFGHJLMNQRTUYZ123456789";
        return randomStr(baseChar,length);
    }

    public static void main(String[] args) {
        System.out.println(randomStr(6));
    }
}
